<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  
		xmlns:h="http://java.sun.com/jsf/html"  
		xmlns:f="http://java.sun.com/jsf/core"
		xmlns:ui="http://java.sun.com/jsf/facelets"
		xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:a4j="http://richfaces.org/a4j"
        xmlns:rich="http://richfaces.org/rich">  
    
    <ui:composition template="#{helperNavigation.getPathTemplate('basic')}">
        <ui:define name="pageTitle">Panier</ui:define>
        <ui:define name="mainTitle">Contenu de votre panier</ui:define>
        <ui:define name="mainContent">
            <c:if test="#{cartSession.invoices == null || cartSession.invoices.isEmpty()}">
                <div class="alert alert-info div-info"><h4>Votre panier est vide.</h4></div>
            </c:if>
            <c:if test="#{cartSession.invoices != null}">
                <c:if test="#{!cartSession.invoices.isEmpty()}">
                    <div class="center">
                        <h:form id="formCart">
                            <rich:dataTable value="#{cartSession.invoices}" var="invoice" rows="10" rowClasses="odd-row, even-row" style="width: 100%;">
                                <f:facet name="header">
                                    <rich:columnGroup>
                                        <rich:column>
                                            <h:outputText value="Titre" />
                                        </rich:column>
                                        <rich:column>
                                            <h:outputText value="Quantité" />
                                        </rich:column>
                                        <rich:column>
                                            <h:outputText value="Prix unitaire" />
                                        </rich:column>
                                        <rich:column>
                                            <h:outputText value="Sous-total" />
                                        </rich:column>
                                        <rich:column>
                                            <h:outputText value="Supprimer du panier" />
                                        </rich:column>
                                    </rich:columnGroup>
                                </f:facet>

                                <rich:column>
                                    <h:outputText value="#{invoice.book.title}" />
                                </rich:column>
                                <rich:column>
                                    <h:inputText value="#{invoice.quantity}" styleClass="validate[required, custom[onlyNumberSp], min[1], max[#{invoice.book.stock}]], inputUpdateCart" />
                                </rich:column>
                                <rich:column>
                                    <h:outputText value="#{invoice.book.price} €" />
                                </rich:column>
                                <rich:column>
                                    <h:outputText value="#{invoice.book.price * invoice.quantity} €" />
                                </rich:column>
                                <rich:column>
                                    <h:commandLink value="Supprimer" action="#{cartSession.removeInvoice(invoice)}" styleClass="red b" />
                                </rich:column>

                                <f:facet name="footer">
                                    <rich:columnGroup>
                                        <rich:column colspan="4">
                                            <rich:dataScroller rendered="#{(cartSession.invoices.size() > 10)? true : false}" />
                                        </rich:column>
                                        <rich:column colspan="1">Prix total :&nbsp;&nbsp;<h:outputText value="#{cartSession.totalPrice} €" styleClass="label"></h:outputText></rich:column>
                                    </rich:columnGroup>
                                </f:facet>
                            </rich:dataTable>

                            <div styleClass="fltrt">
                                <hr />
                                <span class="btn btn-warning"><i class='icon-repeat' onclick="$j(this).parent().children(':last').click();"></i>&nbsp;<h:commandLink value="Mettre à jour" action="#{cartSession.updateCart()}" styleClass="updateCart" /></span>
                                <span class="btn btn-success"><i class='icon-ok' onclick="$j(this).parent().children(':last').click();"></i>&nbsp;<h:commandLink value="Commander" action="#{orderManager.createOrder()}" styleClass="validateCart" /></span>
                            </div>
                        </h:form>
                    </div>
                </c:if>
            </c:if>
            
            <br />
            <hr />
            <ul class="marginLeftList">
                <li>
                    <h:form>
                        <h:commandLink action="#{bookManager.loadAllBooks}">
                            <h:outputText value="Consulter la bibliothèque." />
                        </h:commandLink>
                    </h:form>
                </li>
                <c:if test="#{userSession.isLogged}">
                    <li>
                        <h:form>
                            <h:commandLink action="#{orderManager.setAllOrders()}">
                                <h:outputText value="Liste des commandes récentes." />
                            </h:commandLink>
                        </h:form>
                    </li>
                    <li>
                        <h:form>
                            <h:commandLink action="#{orderManager.setWaitingOrders()}">
                                <h:outputText value="Liste des commandes en cours de traitement." />
                            </h:commandLink>
                        </h:form>
                    </li>
                </c:if>
            </ul>
                
            <script type="text/javascript">
                $j(function(){
                    var form = "#formCart";

                    // Load validationEngine for check in real time the user entries.
                    $j(form).validationEngine('attach');

                    // AutoUpdate cart when press enter key on input text.
                    $j('.inputUpdateCart').keypress(function(e){
                        console.log("Oui");
                        ($j(form).validationEngine('validate'))? (e.keyCode == 13)? $j('.updateCart').click() : "" : "" ;
                    });

                });
            </script>
        </ui:define>
    </ui:composition>
</html>

